/**
 * 
 */
package com.ra.wr;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.da.landlord.DaSuite;
import com.googlecode.cswish.annotation.Rest;
import com.googlecode.cswish.model.PageInfo;
import com.googlecode.cswish.struts.spring.GenericService;
import com.ra.external.RoomInfoService;
import com.ra.landlord.Building;
import com.ra.landlord.LandlordAssistUser;
import com.ra.landlord.LandlordUser;
import com.ra.landlord.LandlordUserService;

/**
 * 房源发布服务接口. 对com.ra.external。RoomInfoService的包装调用
 * @author Jebel Qin
 * @date 2015年8月24日
 * @since v4.0
 */

@Service
public class RoomPublishService
{
	@Resource
	private GenericService genericService;
	
	@Resource
	private RoomInfoService roomInfoService;
	
	@Resource
	private LandlordUserService landlordUserService;
	
	/**
	 * 发布房源
	 * @param model
	 * @param pageIndex
	 * @param pageSize
	 * @return
	 */
	@Rest(type="search")
	public PageInfo searchAllPublishRoomInfo(RoomInfoTdo model, Integer pageIndex, Integer pageSize)
	{
		//对管理员的处理
		LandlordUser landlordUser = landlordUserService.getLandlordUser();
		if ((RoomInfoTdo.SEARCH_TYPE_ALL.equals(model.getSearchType()) || 
				RoomInfoTdo.SEARCH_TYPE_DA.equals(model.getSearchType()))
				&& landlordUser instanceof LandlordAssistUser)
		{
			
			LandlordAssistUser landlordAssistUser = genericService.load(LandlordAssistUser.class, landlordUser.getId());
			List<Building> assignedBuildings = landlordAssistUser.getBuildings();
			List<Integer> buildingIds = new ArrayList<Integer>(assignedBuildings.size());
			for (Building building : assignedBuildings) 
			{
				buildingIds.add(building.getId());
			}
			
			DaSuite suite = new DaSuite();
			suite.setLandlordUser(landlordUser.getOwnerUser());
			List<LandlordUser> assists = new ArrayList<LandlordUser>();
			assists.add(landlordAssistUser);
			suite.setLandlordUsers(assists);
			
			List<DaSuite> dbSuiteList = genericService.searchAll(suite).getData();
			List<Integer> suiteIds = new ArrayList<Integer>(assignedBuildings.size());
			for (DaSuite dbSuite : dbSuiteList) 
			{
				suiteIds.add(dbSuite.getId());
			}
			model.setBuildingIds(buildingIds);
			model.setSuiteIds(suiteIds);
		}
		
		return roomInfoService.searchAllPublishRoomInfo(model, pageIndex, pageSize);
	}
	
	
}
